package com.mts.javacommon.jpabase;

import java.io.Serializable;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 *
 *
 * <pre>
 *      ResultVo
 * </pre>
 *
 * @author Johnny
 * @date 2021-09-28
 */
@Data
@Schema(name = "结果对象")
public class ResultVo<T> implements Serializable {

  /** 错误码. */
  private Integer code;

  /** 提示信息. */
  private String msg;

  /** 具体内容. */
  private T data;

  /**
   * success
   *
   * @param o
   * @return
   */
  public static <T> ResultVo<T> success(T o) {
    ResultVo<T> resultVO = instance();
    resultVO.setCode(200);
    resultVO.setMsg(ResultEnum.SUCCESS.name());
    resultVO.setData(o);
    return resultVO;
  }

  /**
   * error
   *
   * @param o
   * @return
   */
  public static <T> ResultVo<T> error(T o) {
    ResultVo<T> resultVO = instance();
    resultVO.setCode(500);
    resultVO.setMsg(ResultEnum.ERROR.name());
    resultVO.setData(o);
    return resultVO;
  }

  @SuppressWarnings("unchecked")
  private static <T> ResultVo<T> instance() {
    return new ResultVo<T>();
  }

  public enum ResultEnum {
    /** 成功 */
    SUCCESS,
    /** 失败 */
    ERROR;
  }
}
